<?php

/**
 * This is the model class for table "member_code".
 *
 * The followings are the available columns in table 'member_code':
 * @property string $code
 * @property integer $user_id
 * @property string $create_at
 *
 * The followings are the available model relations:
 * @property Education[] $educations
 * @property Users $user
 */
class MemberCode extends MemberCodeBase
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return MemberCode the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'code' 		=> 'Mã sinh viên',
			'user_id' 	=> 'User ID',
			'courses_id'=> 'Khóa thành viên',
			'status' 	=> 'Thành viên nhận học bổng',
			'create_at' => 'Ngày tạo Member code',
			'active'=> 'Kích hoạt',
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array_merge(parent::relations(),array(
			'education' => array(self::HAS_MANY, 'Education', 'code_id'),
			'courses' 	=> array(self::BELONGS_TO, 'Courses', 'courses_id'),
		));
	}

    public function getCode($userId) {
		$criteria=new CDbCriteria;

		$criteria->compare('active', 1);
		$criteria->compare('user_id',$userId);

		return $this->find($criteria);
    }

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('code',$this->code,true);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('courses_id',$this->courses_id);
		$criteria->compare('status',$this->status);
		$criteria->compare('create_at',$this->create_at,true);
		$criteria->compare('active',$this->active);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}